<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<section class="content-header">
    <h1>
        产品下单
    </h1>
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <!-- general form elements -->
            <div class="box box-default">
                <!-- /.box-header -->
                <!-- form start -->
                <form id="form" role="form" class="form-horizontal">
                    <input type="hidden" value="" name="id">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="input1" class="col-sm-4 control-label">产品名称</label>
                            <div class="col-sm-6">
                            <input id="input1" class="form-control" type="text" value="" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input2" class="col-sm-4 control-label">销售单价</label>
                            <div class="col-sm-6">
                            <input id="input2" class="form-control" type="text" value="" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input3" class="col-sm-4 control-label">购买数量</label>
                            <div class="col-sm-6">
                            <input id="input3" class="form-control" type="text" value="" name="num">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input4" class="col-sm-4 control-label">购买单价</label>
                            <div class="col-sm-6">
                            <input id="input4" class="form-control" type="text" value="" name="price">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input5" class="col-sm-4 control-label">订单总价</label>
                            <div class="col-sm-6">
                            <input id="input5" class="form-control" type="text" value="" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input6" class="col-sm-4 control-label">进口税</label>
                            <div class="col-sm-6">
                            <input id="input6" class="form-control" type="text" value="" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input7" class="col-sm-4 control-label">营销总监</label>
                            <div class="col-sm-6">
                                <select id="input7" class="form-control" name="member_id">
                                    <option value="" disabled selected>请选择营销总监</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->

                    <div class="box-footer text-center">
                        <button type="submit" class="btn btn-success">下单</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>



<script>


var userProduct;
$.ajax('/api/product/user-detail', {
    type: 'get',
    data: {
        'id': $.query2object().product_id,
    },
    dataType: 'json',
    headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
}).then(function(ret) {
    if(ret.state ==0) {
        userProduct = ret.data.model;

        $('#input1').val(userProduct.product.name);
        $('#input2').val(userProduct.sale_price);
        $('[name="id"]').val(userProduct.id);
    } else {
        alert(ret.message);
    }
});

$('#input3,#input4').bind('input propertychange',function(){
    $('#input5').val($('#input3').val() * $('#input4').val());
    if(userProduct.user.country.id != global.user.country.id) {
        $('#input6').val($('#input3').val() * $('#input4').val() * global.user.country.import_duty);
    } else {
        $('#input6').val(0);
    }
})

$(function() {
    if(global.user.role==4) {
        $('#input7').closest('.form-group').hide();
    }
})


$.ajax('/api/member/list', {
    type: 'get',
    data: {
        position: 3,
    },
    dataType: 'json',
    headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
}).then(function (ret) {
    for (var i in ret.data.list) {
        $('#input7').append($('<option>').attr('value', ret.data.list[i].id).text(ret.data.list[i].name));
    }
});



$('#form').submit(function(e) {
    e.preventDefault();
    var str = $('#form').serialize();
    if(!userProduct) {
        return alert('加载未完成');
    }

    $.ajax('/api/order/create', {
        type: 'POST',
        data: str,
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('保存成功');
            location.href = '/order/index';
        } else {
            alert(ret.message);
        }
    });
});

</script>